package com.example.rubbish.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.rubbish.DataBase.HDataBase;
import com.example.rubbish.DataBase.History;
import com.example.rubbish.R;
import com.example.rubbish.ResultActivity;
import com.google.android.flexbox.FlexboxLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * part_history(LinearLayout.vertical):
 * *         {
 * *             top_margin2(View);
 * *             history_hint(ConstraintLayout):
 * *             {
 * *                 textview_history(TextView);
 * *                 history_del(Button);
 * *             };
 * *             flexBox_history(FlexboxLayout):
 * *             {
 * *                 ;
 * *             };
 * *         }
 */
public class HistoryFragment extends Fragment {
    private HDataBase db;
    private Button button_historyDel;
//    private LinearLayout box_history;
//    private ConstraintLayout box_history;
//    private Flow flow_history;
    private FlexboxLayout box_history;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_history, container, false);
        db= HDataBase.getInstance(getContext());
        button_historyDel = view.findViewById(R.id.history_del);
        box_history=view.findViewById(R.id.box_history);
//        flow_history=box_history.findViewById(R.id.flow_history);

        loadHistoryView();

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setChildOnClickListener();
        setDelOnClickListener();
    }

    private void setDelOnClickListener() {
        button_historyDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delAllViews();
            }
        });
    }


    private void setChildOnClickListener() {
        for (int i = 0; i < box_history.getChildCount(); i++) {
            TextView tv = (TextView) box_history.getChildAt(i);
            String searchContent = tv.getText().toString();
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), ResultActivity.class);
                    intent.putExtra("searchContent", searchContent);
                    intent.putExtra("searchType",0);
                    startActivity(intent);
                }
            });
        }
    }
    private void delAllViews() {
        box_history.removeAllViews();
        db.delAllHistory();

    }
    public void loadHistoryView() {
        box_history.removeAllViews();
        List<History> historyList = new ArrayList<>();
//        List<Integer> idList = new ArrayList<>();
        historyList = db.loadHistory();
        for (int i = 0; i < historyList.size(); i++) {
            History history = historyList.get(i);
            String searchContent = history.getHistoryName();
//            Button tv = new Button(getContext());
            TextView tv = new TextView(getContext());
            tv.setId(View.generateViewId());
            tv.setText(searchContent);
            tv.setBackground(getResources().getDrawable(R.drawable.shape_button_circular, null));
            tv.setTextSize(15f);
            tv.setBackgroundColor(getResources().getColor(R.color.grey));
            tv.setMaxLines(1);
            FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(10, 10,10, 10);
            tv.setLayoutParams(lp);

            box_history.addView(tv);
//            idList.add(tv.getId());
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    jumpToResult(searchContent);
                }
            });
        }


    }

    private void jumpToResult(String searchContent) {
        Intent intent = new Intent(getContext(), ResultActivity.class);
        intent.putExtra("searchContent", searchContent);
        intent.putExtra("searchType",0);
        startActivity(intent);
    }


}//class

//    public void refreshUI() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Message message = new Message();
//                message.what=REFRESH;
//                handler.sendMessage(message);
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                Log.i("history","更新了UI");
//            }
//        });
//
//
//    }
